//
//  ManagerAFNetWorking.m
//  TaoLeSong
//
//  Created by EmberWei on 2017/07/03.
//  Copyright © 2017年 LimeKing. All rights reserved.
//

#import "ManagerAFNetWorking.h"
#import "Response.h"
#import "Api.h"

@implementation ManagerAFNetWorking

+ (ManagerAFNetWorking *)manager
{
    static ManagerAFNetWorking *sharedNetworkManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^
                  {
                      NSString *domain = [Singleton shareInstance].isProduct ? ENV_PROD_DOMAIN : ENV_TEST_DOMAIN;
                      sharedNetworkManager = [[ManagerAFNetWorking alloc] initWithBaseURL:[NSURL URLWithString:domain]];
                      sharedNetworkManager.requestSerializer = [AFHTTPRequestSerializer serializer];
                      sharedNetworkManager.requestSerializer.timeoutInterval = 10.f;
                      [sharedNetworkManager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
                      sharedNetworkManager.responseSerializer = [AFJSONResponseSerializer serializer];
                      sharedNetworkManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];;
                  });
    return sharedNetworkManager;
}

+ (void)json
{
    [ManagerAFNetWorking manager].requestSerializer = [AFJSONRequestSerializer serializer];
    [ManagerAFNetWorking manager].requestSerializer.timeoutInterval = 10.f;
    [[ManagerAFNetWorking manager].requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
}

+ (void)form
{
    [ManagerAFNetWorking manager].requestSerializer = [AFHTTPRequestSerializer serializer];
    [ManagerAFNetWorking manager].requestSerializer.timeoutInterval = 10.f;
    [[ManagerAFNetWorking manager].requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
}

@end

@implementation HttpObject


+ (NSURLSessionTask *)POST:(NSString *)urlString parameters:(id)parameters responseClass:(Class)responseClass handler:(HttpHandler)handler {
    NSLog(@"POST:%@请求参数:%@", urlString, parameters);
    return [[ManagerAFNetWorking manager] POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [[self class] analyseWithUrlString:urlString data:responseObject responseClass:responseClass handler:handler];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        BLOCK_EXEC(handler,nil,error);
    }];
}

+ (NSURLSessionTask *)GET:(NSString *)urlString parameters:(id)parameters responseClass:(Class)responseClass handler:(HttpHandler)handler {
    NSLog(@"GET:%@请求参数:%@", urlString, parameters);
    return [[ManagerAFNetWorking manager] GET:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [[self class] analyseWithUrlString:urlString data:responseObject responseClass:responseClass handler:handler];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        BLOCK_EXEC(handler,nil,error);
    }];
}

+ (NSURLSessionTask *)POST:(NSString *)urlString parameters:(id)parameters
 constructingBodyWithBlock:(void (^)(id<AFMultipartFormData> _Nonnull))block
                  progress:(void (^)(NSProgress * _Nonnull))uploadProgress
             responseClass:(Class)responseClass
                   handler:(HttpHandler)handler {
    NSLog(@"上传文件:%@请求参数:%@", urlString, parameters);
    return [[ManagerAFNetWorking manager] POST:urlString parameters:parameters constructingBodyWithBlock:block progress:uploadProgress success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [[self class] analyseWithUrlString:urlString data:responseObject responseClass:responseClass handler:handler];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        BLOCK_EXEC(handler,nil,error);
    }];
}

+ (NSURLSessionTask *)PATCH:(NSString *)urlString parameters:(id)parameters responseClass:(Class)responseClass handler:(HttpHandler)handler {
    NSLog(@"PATCH:%@请求参数:%@", urlString, parameters);
    return [[ManagerAFNetWorking manager] PATCH:urlString parameters:parameters  success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
           [[self class] analyseWithUrlString:urlString data:responseObject responseClass:responseClass handler:handler];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        BLOCK_EXEC(handler,nil,error);
    }];
}

+ (NSURLSessionTask *)PUT:(NSString *)urlString parameters:(id)parameters responseClass:(Class)responseClass handler:(HttpHandler)handler {
    NSLog(@"PUT:%@请求参数:%@", urlString, parameters);
    return [[ManagerAFNetWorking manager] PUT:urlString parameters:parameters  success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [[self class] analyseWithUrlString:urlString data:responseObject responseClass:responseClass handler:handler];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        BLOCK_EXEC(handler,nil,error);
    }];
}

+ (NSURLSessionTask *)DELETE:(NSString *)urlString parameters:(id)parameters responseClass:(Class)responseClass handler:(HttpHandler)handler {
    NSLog(@"DELETE:%@请求参数:%@", urlString, parameters);
    return [[ManagerAFNetWorking manager] DELETE:urlString parameters:parameters  success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [[self class] analyseWithUrlString:urlString data:responseObject responseClass:responseClass handler:handler];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        BLOCK_EXEC(handler,nil,error);
    }];
}
+ (void)analyseWithUrlString:(NSString*)urlString data:(id)data responseClass:(Class)responseClass handler:(HttpHandler)handler {
    [responseClass analyseWithData:data complete:^(__kindof Response *result, NSError *error) {
        NSLog(@"HTTP:%@返回参数：%@",urlString,[result description]);
        BLOCK_EXEC(handler, result, error);
    }];
}

@end
